package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrederServiceImpl implements OrderService {
    @Autowired
    OrderDao orderDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public Result addOrder(Map map) {
        String telephone = (String) map.get("telephone");
        String name = (String) map.get("name");
        String idCard = (String) map.get("idCard");
        String sex = (String) map.get("sex");

        Date orderDate = null;
        try {
            orderDate = new SimpleDateFormat("yyyy-MM-dd").parse((String)map.get("orderDate"));
        } catch (ParseException e) {
            e.printStackTrace();
            return new Result(false,"日期格式错误");
        }

        String setmealId = (String) map.get("setmealId");
        String orderType = (String) map.get("orderType");

        //通过号码查看预约人是否已是会号
        Member member = memberDao.findByTel(telephone);
        if (member == null) {// 不是会员
            member=new Member();
            member.setName(name);
            member.setPhoneNumber(telephone);
            member.setSex(sex);
            member.setIdCard(idCard);
            memberDao.add(member);//添加为会员
        }

        Integer memberId = member.getId();//得到会员号

        //得到预约日期的预约设置信息
        OrderSetting orderSetting = orderSettingDao.findByDate(orderDate);
        //预约日期是否能预约
        if (orderSetting!=null){
            //当已预约人数小于设置人数时
            if (orderSetting.getReservations() < orderSetting.getNumber()) {
                //添加预约信息
                Order order = new Order();
                order.setSetmealId(Integer.parseInt(setmealId));
                order.setOrderDate(orderDate);
                order.setOrderType(orderType);
                order.setMemberId(memberId);

                //判断预约人日否已在预约日预约
                if (hasOredered(order)){
                    return new Result(false,MessageConstant.HAS_ORDERED);
                }
                //添加预约信息
                orderDao.add(order);

                //增加已预约人数
                orderSetting.setReservations(orderSetting.getReservations()+1);
                orderSettingDao.update(orderSetting);
                return new Result(true,MessageConstant.ORDER_SUCCESS,order);
            } else {
                return new Result(false,MessageConstant.ORDER_FULL);
            }
        }else {
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

    }

    @Override
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }

    @Override
    public List<Map> getSetmealReport() {
        return orderDao.getSetmealReport();
    }

    private boolean hasOredered(Order order){
        List<Order> ordersDB=orderDao.findByCondition(order);
        if (ordersDB.size()>0){
            return true;
        }
        return false;
    }
}
